import 'package:flutter/material.dart';
import 'package:trip_flutter/dao/loginDao.dart';
import 'package:trip_flutter/util/screen_adapt_helper.dart';
import 'package:trip_flutter/widget/localCard.dart';
import 'package:trip_flutter/widget/localEntry.dart';
import 'package:trip_flutter/widget/sub_nav_grid.dart';

import '../widget/pomCard.dart';
import '../widget/slideWidget.dart';

class Homepage extends StatefulWidget {
  const Homepage({super.key});
  @override
  State<Homepage> createState() => _HomepageState();
}

class _HomepageState extends State<Homepage>
    with AutomaticKeepAliveClientMixin {
  List<String> bannerList = [
    'images/trip_01.jpg',
    'images/trip_02.jpg',
    'images/trip_03.jpg',
    'images/trip_04.jpg',
    'images/trip_05.jpg',
  ];
  double appBarAlpha = 0;
  static double appBarOffset = 100;
  get logoutButton => TextButton(
    onPressed: () {
      debugPrint("登出按钮被点击");
      LoginDao.logout();
    },
    child: Text("登出", style: TextStyle(color: Colors.blue)),
  );

  @override
  Widget build(BuildContext context) {
    //初始化适配工具
    ScreenHelper.init(context);
    return Scaffold(
      //防止键盘起来影响布局
      resizeToAvoidBottomInset: false,
      body: Stack(
        children: [
          MediaQuery.removePadding(
            removeTop: true,
            context: context,
            child: NotificationListener(
              onNotification: (scrollNotification) {
                if (scrollNotification is ScrollUpdateNotification &&
                    scrollNotification.depth == 0) {
                  //触发alpha更新
                  _inScroll(scrollNotification.metrics.pixels);
                }
                return false;
              },
              child: _listView,
            ),
          ),
          _appBar,
        ],
      ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
  get _appBar => Opacity(
    opacity: appBarAlpha,
    child: Container(
      height: 80,
      decoration: BoxDecoration(color: Colors.white),
      child: Center(
        child: Padding(
          padding: EdgeInsets.only(top: 20.px),
          child: Stack(
            children: [
              // 首页文本居中显示
              const Center(
                child: Text(
                  "首页",
                  style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                ),
              ),
              // 登出按钮固定在右侧
              Positioned(
                right: 15, // 右侧距离
                top: 0,
                bottom: 0,
                child: Center(
                  // 确保按钮在垂直方向居中
                  child: logoutButton,
                ),
              ),
            ],
          ),
        ),
      ),
    ),
  );

  get _listView => ListView(
    children: [
      Slidewidget(bannerList: bannerList),
      localWidget(),
      Localcard(),
      SubNavGrid(),
      PromoCard(),
    ],
  );

  void _inScroll(double pixels) {
    double alpha = pixels / appBarOffset;
    if (alpha < 0) {
      alpha = 0;
    } else if (alpha > 1) {
      alpha = 1;
    }
    setState(() {
      appBarAlpha = alpha;
    });
  }
}
